package com.tianzj.mvc.system.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.mvc.system.pojo.SysUser;
import com.tianzj.mvc.system.service.SysUserService;
import com.tianzj.util.AjaxJson;
import com.tianzj.util.DateUtils;
import com.tianzj.util.JsonUtil;
import com.tianzj.util.MD5Utils;
import com.tianzj.util.Page;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {
	
	private Logger logger  =  LoggerFactory.getLogger(SysUserController.class );
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/userList")
	public ModelAndView userList(){
		return new ModelAndView("system/user/userList");
	}
	
	@RequestMapping("/getUserList")
	public Object getUserList(Page page,SysUser user){
		List<SysUser> users = sysUserService.getUserList(page,user);
		return JsonUtil.list2BootStrapJson(users);
	}
	
	@RequestMapping("/addSysUser")
	public ModelAndView addSysUser(SysUser user){
		try {
			sysUserService.addSysUser(user);
		} catch (Exception e) {
			logger.error("新增用户失败"+e.getMessage());
		}
		return new ModelAndView("redirect:/sysUser/userList");
	}
	
	/**
	 * 编辑用户信息
	 * @param sysUser
	 * @return
	 */
	@PostMapping("editSysUser")
	public ModelAndView editSysUser(SysUser sysUser){
		try {
			sysUser.setUpdateDate(System.currentTimeMillis());
			sysUser.setPassword(MD5Utils.MD5(sysUser.getPassword()));
			sysUserService.updateUser(sysUser);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}						
		return new ModelAndView("redirect:/sysUser/userList");
	}
	
	
	@PostMapping("getSysUserById")
	public SysUser getSysUserById(SysUser sysUser){
		return sysUserService.getSysUserById(sysUser.getId());
	}
	
	@PostMapping("removeById")
	public AjaxJson removeById(String ids){
		AjaxJson json=new AjaxJson();
		try {
			json.setMsg("删除成功");
			sysUserService.deleteSysUserById(ids);
		} catch (Exception e) {
			logger.error(e.getMessage());
			json.setMsg("删除失败");
		}
		return json;
	}
	
	public static void main(String[] args) {
		Date date = new Date(1530785363367L);
		System.out.println(DateUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
		Date date2 = new Date(1530785364355L);
		System.out.println(DateUtils.format(date2, "yyyy-MM-dd HH:mm:ss"));
	}
	
}
